package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.whut.mapper.*;
import com.whut.model.*;
import com.whut.model.Contracttaskaccept;
import com.whut.model.Problemcollect;
import com.whut.service.MotorService;
import com.whut.v_model.PointAndProject;
import com.whut.v_model.CTPInfo;
import com.whut.v_model.ProblemCollectDetail;
import com.whut.v_model.ProjectNum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("motorService")
public class MotorServiceImpl implements MotorService {
    @Resource
    private ContracttaskacceptMapper contracttaskacceptMapper;
    @Resource
    private NewprefixsystemMapper newprefixsystemMapper;
    @Resource
    private ProblemcollectMapper problemcollectMapper;
    @Resource
    private ContractchecktaskMapper contractchecktaskMapper;
    @Resource
    private AllpointsaveMapper allpointsaveMapper;
    @Resource
    private ContractinfoMapper contractinfoMapper;
    @Resource
    private ContracttaskprojectMapper contracttaskprojectMapper;
    @Resource
    private ProblemCollectDetailMapper problemCollectDetailMapper;

    @Override
    public List<Object> getAllContractInfoByReserve1(String reserve1, String contractId){
        JSONArray list = JSONArray.parseArray(contractId);
        List<Integer> result = new ArrayList<Integer>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                System.out.println(list.getInteger(i));
                result.add(list.getInteger(i));
            }
        }
        return contracttaskacceptMapper.getAllContractInfoByReserve1(reserve1,result);
    }

    @Override
    public List<Newprefixsystem> getSecondChildsByNode(Integer preNum) {
        return newprefixsystemMapper.getNextLevel(preNum);
    }

    @Override
    public Integer getProblemNumByForthId(Integer fourthId,String contractId) {
        JSONArray list = JSON.parseArray(contractId);
        List<Integer> result = new ArrayList<Integer>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                System.out.println(list.getInteger(i));
                result.add(list.getInteger(i));
            }
        }
        return problemcollectMapper.getProblemNumByForthId(fourthId,result);
    }

    @Override
    public List<Problemcollect> getItemByNameAndDepth(String parameterName, Integer nodeDepth,String contratcId) {
        JSONArray list =JSON.parseArray(contratcId);
        List<Integer> result = new ArrayList<Integer>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                result.add(list.getInteger(i));
            }
        }
        return newprefixsystemMapper.getItemByNameAndDepth(parameterName,nodeDepth,result);
    }

    @Override
    public List<Problemcollect> getProblemsByForthId(Integer fourthId) {
        return problemcollectMapper.getProblemsByForthId(fourthId);
    }


    @Override
    public Integer getMaxTaskId(Integer contractId) {
        return contracttaskacceptMapper.getMaxTaskId(contractId);
    }

    @Override
    public List<ContractCheckTask> getCheckBytaskId(Integer taskId) {
        return contractchecktaskMapper.getCheckBytaskId(taskId);
    }

    @Override
    public List<Problemcollect> getProblemsBycheckId(Integer checkId) {
        return problemcollectMapper.getProblemsBycheckId(checkId);
    }

    @Override
    public List<Object> getProblemsByContractId(Integer contractId) {

        return problemcollectMapper.getProblemsByContractId(contractId);
    }

    @Override
    public List<Object> getAllContractInfo() {
        return contractinfoMapper.getAllContractInfo();
    }

    @Override
    public List<ContractTaskProject> getAllProjectInfo() {
        return contracttaskprojectMapper.getAllProject();
    }

    @Override
    public List<Problemcollect> getProjectInfoByContractId(Integer contractId,Integer firstId) {
        return contracttaskprojectMapper.getProjectInfoByContractId(contractId,firstId);
    }

    @Override
    public Problemcollect getTimeInterval(Integer contractId, Integer firstId) {
        return problemcollectMapper.getTimeInterval(contractId,firstId);
    }

    @Override
    public Contractinfo getClientByContractId(Integer contractId) {
        return contractinfoMapper.getClientByContractId1(contractId);
    }

    @Override
    public List<ProblemCollectDetail> getRiskWarning(Integer contractId){
        return problemCollectDetailMapper.getRiskWarning(contractId);
    }

    @Override
    public List<PointAndProject> GetProjectRank(Integer taskId,String startTime, String endTime){
        List<PointAndProject> res = allpointsaveMapper.GetProjectRank(taskId,startTime,endTime);
        List<PointAndProject> pt = contractchecktaskMapper.getFinishTimeForProject();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<pt.size();j++){
                if(res.get(i).projectId.equals(pt.get(j).projectId)){
                    PointAndProject pp = res.get(i);
                    pp.setFinishTime(pt.get(j).getFinishTime());
                }
            }
        }
        System.out.println(res);
        return res;
    }

    @Override
    public List<PointAndProject> getNewProjectRank(){
        List<PointAndProject> res = allpointsaveMapper.getNewProjectRank();
        List<PointAndProject> pt = contractchecktaskMapper.getFinishTimeForProject();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<pt.size();j++){
                if(res.get(i).projectId.equals(pt.get(j).projectId)){
                     PointAndProject pp = res.get(i);
                     pp.setFinishTime(pt.get(j).getFinishTime());
                }
            }
        }
        return res;
    }

    @Override
    public List<PointAndProject> getNewProjectOnlyByTime(String startTime, String endTime){
        return allpointsaveMapper.getNewProjectOnlyByTime(startTime, endTime);
    }

    @Override
    public List<PointAndProject> getNewProjectRankByContract(Integer contractId){
        List<PointAndProject> res = allpointsaveMapper.getNewProjectRankByContract(contractId);
        List<PointAndProject> pt = contractchecktaskMapper.getFinishTimeForProject();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<pt.size();j++){
                if(res.get(i).projectId.equals(pt.get(j).projectId)){
                    PointAndProject pp = res.get(i);
                    pp.setFinishTime(pt.get(j).getFinishTime());
                }
            }
        }
        return res;
    }

    @Override
    public List<Contracttaskaccept> getAllTaskInfo(Integer contractId){
        return contracttaskacceptMapper.getAllTaskInfo(contractId);
    }

    @Override
    public List<Contracttaskaccept> getAllTask(){
        return contracttaskacceptMapper.getAllTask();
    }

    @Override
    public List<ProblemCollectDetail> getProblemByProject(Integer checkId,Integer projectId){ return problemCollectDetailMapper.getProblemByProject(checkId,projectId);}

    @Override
    public List<ProjectNum> getProjectNumByArea(String area) {
        return contracttaskprojectMapper.getProjectNumByArea(area);
    }

    @Override
    public Object getProjectLocByContractIds(String contractIds) {
        JSONArray list =JSON.parseArray(contractIds);
        List<Integer> result = new ArrayList<Integer>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                result.add(list.getInteger(i));
            }
        }
        return contracttaskprojectMapper.getProjectLocByContractIds(result);
    }

    @Override
    public List<CTPInfo> getCTPInfoByContractId(Integer contractId) {
        return contracttaskprojectMapper.getCTPInfoByContractId(contractId);
    }

    @Override
    public List<Object> getDataTest(String secondName, String contractIds) {
        JSONArray list =JSON.parseArray(contractIds);
        List<Integer> result = new ArrayList<Integer>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                result.add(list.getInteger(i));
            }
        }

        return problemCollectDetailMapper.getAllItemsBySecondName(secondName,result);
    }

    @Override
    public List<ProblemCollectDetail> getProblemDetail(int projectId, String reserve1) {
        return problemCollectDetailMapper.getProblemDetail(projectId,reserve1);
    }

    @Override
    public List<ProblemCollectDetail> getProblemsByTaskId(int taskId) {
        return problemCollectDetailMapper.getProblemsByTaskId(taskId);

    }

}
